package io.github.noahzu.daka

import android.content.Intent
import android.view.View
import androidx.appcompat.app.ActionBar
import cn.leancloud.AVObject
import cn.leancloud.AVQuery
import com.blankj.utilcode.util.ToastUtils
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_login_user.*
import kotlinx.android.synthetic.main.activity_main.*

class LoginUserActivity : BaseActivity() {
    override fun layoutId(): Int = R.layout.activity_login_user

    override fun beforeCreate() {
        val actionBar: ActionBar? = supportActionBar
        actionBar?.hide()
    }

    override fun afterCreate() {
        doAddOnClick()
    }

    private fun doAddOnClick() {
        mLoginBtn.setOnClickListener {
            doLogin()
        }
        mRegisterBtn.setOnClickListener {
            doRegisater()
        }
    }

    private fun doRegisater() {
        if(mEditUserText.text.isNullOrEmpty() || mEditPasswordText.text.isNullOrEmpty()) {
            ToastUtils.showLong("用户名或密码不能为空")
            return
        }
        mProgressBar.visibility = View.VISIBLE
        val avobj = AVObject("LoginUser_")
        avobj.put("mUserName",mEditUserText.text.toString())
        avobj.put("mPassword",mEditPasswordText.text.toString())
        avobj.saveInBackground()
            .subscribeOn(Schedulers.io())//这里指定在io线程执行
            .observeOn(AndroidSchedulers.mainThread())//返回结果在主线程执行
            .subscribe(object : Observer<AVObject> {
                override fun onComplete() = Unit

                override fun onSubscribe(d: Disposable) = Unit

                override fun onNext(t: AVObject) {
                    ToastUtils.showShort("注册成功")
                    UserManager.saveLogin(t.getString("mUserName"))
                    mProgressBar.visibility = View.GONE
                    startActivity(Intent(this@LoginUserActivity,MainActivity::class.java))
                    finish()
                }

                override fun onError(e: Throwable) {
                    ToastUtils.showShort("注册失败")
                    mProgressBar.visibility = View.GONE
                }

            })
    }

    private fun doLogin() {
        if(mEditUserText.text.isNullOrEmpty() || mEditPasswordText.text.isNullOrEmpty()) {
            ToastUtils.showLong("用户名或密码不能为空")
            return
        }

        mProgressBar.visibility = View.VISIBLE
        val query = AVQuery<AVObject>("LoginUser_")
        query
            .whereEqualTo("mUserName",mEditUserText.text.toString())
            .whereEqualTo("mPassword",mEditPasswordText.text.toString())
            .findInBackground()
            .subscribeOn(Schedulers.io())//这里指定在io线程执行
            .observeOn(AndroidSchedulers.mainThread())//返回结果在主线程执行
            .subscribe(object : Observer<List<AVObject>> {
                override fun onSubscribe(disposable: Disposable) {

                }
                override fun onError(throwable: Throwable) {
                    ToastUtils.showShort("登录失败")
                    mProgressBar.visibility = View.GONE
                }
                override fun onComplete() {

                }
                override fun onNext(t: List<AVObject>) {
                    if(t.isEmpty()) {
                        ToastUtils.showShort("登录失败")
                        mProgressBar.visibility = View.GONE
                        return
                    }
                    UserManager.saveLogin(t.first().getString("mUserName"))
                    ToastUtils.showShort("登录成功")
                    mProgressBar.visibility = View.GONE
                    startActivity(Intent(this@LoginUserActivity,MainActivity::class.java))
                    finish()
                }
            })
    }

}
